package vn.com.ltv.lo.daos;

import java.util.ArrayList;

import vn.com.ltv.lo.dtos.SetListDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SetListDAO {
	protected static final String TABLE_NAME = "Set_list";
	protected static final String ID = "rowid";
	protected static final String SET_LIST_ID = "set_list_id";
	protected static final String SET_ID = "set_id";
	protected static final String DSH_ID = "dsh_id";
	protected static String TABLE_CREATION_STRING = "create table "
			+ TABLE_NAME + " (" + ID + " integer primary key autoincrement, "
			+ SET_LIST_ID + " text, " + SET_ID + " text, " + DSH_ID + " text);";
	protected static String TABLE_DROP_STRING = "drop table if exists"
			+ TABLE_NAME + ";";
	private Context context;

	public SetListDAO(Context context) {
		this.context = context;
	}

	public ArrayList<SetListDTO> getAll() {
		ArrayList<SetListDTO> setListDTOs = new ArrayList<SetListDTO>();
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, null, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			while (!cursor.isAfterLast()) {
				SetListDTO setListDTO = this.cursorToSetListDTO(cursor);
				setListDTOs.add(setListDTO);
				cursor.moveToNext();
			}
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return setListDTOs;
	}

	public SetListDTO get(int id) {
		SetListDTO setListDTO = null;
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sQLiteDatabase.query(TABLE_NAME, null, ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor.moveToFirst()) {
			setListDTO = this.cursorToSetListDTO(cursor);
		}
		cursor.close();
		sQLiteDatabase.close();
		databaseHelper.close();
		return setListDTO;
	}

	public void insert(SetListDTO setListDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		ContentValues contentValues = this
				.setListDTOToContentValues(setListDTO);
		sQLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	/** * @return the number of rows affected */
	public int update(SetListDTO setListDTO) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		int id = setListDTO.getId();
		setListDTO.setId(-1);
		ContentValues contentValues = this
				.setListDTOToContentValues(setListDTO);
		int rowCount = sQLiteDatabase.update(TABLE_NAME, contentValues, ID
				+ "=?", new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
		return rowCount;
	}

	public void delete(int id) {
		DatabaseHelper databaseHelper = new DatabaseHelper(this.context);
		SQLiteDatabase sQLiteDatabase = databaseHelper.getWritableDatabase();
		sQLiteDatabase.delete(TABLE_NAME, ID + "=?",
				new String[] { String.valueOf(id) });
		sQLiteDatabase.close();
		databaseHelper.close();
	}

	private SetListDTO cursorToSetListDTO(Cursor cursor) {
		SetListDTO setListDTO = new SetListDTO();
		int id = cursor.getInt(cursor.getColumnIndex(ID));
		setListDTO.setId(id);
		String setListId = cursor.getString(cursor.getColumnIndex(SET_LIST_ID));
		setListDTO.setSetListId(setListId);
		String setId = cursor.getString(cursor.getColumnIndex(SET_ID));
		setListDTO.setSetId(setId);
		String dshId = cursor.getString(cursor.getColumnIndex(DSH_ID));
		setListDTO.setDshId(dshId);
		return setListDTO;
	}

	private ContentValues setListDTOToContentValues(SetListDTO setListDTO) {
		ContentValues contentValues = new ContentValues();
		int id = setListDTO.getId();
		if (id >= 0) {
			contentValues.put(ID, id);
		}
		String setListId = setListDTO.getSetListId();
		if (setListId != null) {
			contentValues.put(SET_LIST_ID, setListId);
		}
		String setId = setListDTO.getSetId();
		if (setId != null) {
			contentValues.put(SET_ID, setId);
		}
		String dshId = setListDTO.getDshId();
		if (dshId != null) {
			contentValues.put(DSH_ID, dshId);
		}
		return contentValues;
	}
}